package org.xdq.demo.emp.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xdq.demo.base.BusinessException;
import org.xdq.demo.base.SysException;
import org.xdq.demo.emp.dao.EmpDao;
import org.xdq.demo.emp.dto.EmpDto;
import org.xdq.demo.emp.model.Employee;
import org.xdq.demo.emp.service.EmpService;
//import redis.clients.jedis.Jedis;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;

@Service
@Transactional //说明该类的每个方法都是事务性的，即每个方法发生异常会回滚
@CacheConfig(cacheNames = "emp")//本类的公共缓存配置
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpDao empDao;

    @Autowired
    private RedisTemplate redisTemplate;

    @Cacheable(key = "targetClass+'.'+methodName+'('+#empDto+')' " )//本方法的返回值将被缓存
    @Override
    public List<Employee> getEmpList(EmpDto empDto) {

        try{
            return empDao.findEmpList(empDto);
        }catch (Exception e){
            throw new SysException("系统错误！",e);
        }

//        try{
//            //byte[] key = empDto.toString().getBytes("UTF-8");
//            String key = empDto.toString();
//            Object obj = redisTemplate.opsForValue().get(key);
//            if(obj == null){
//                List<Employee> list = empDao.findEmpList(empDto);
//                redisTemplate.opsForValue().set(key, list);
//                return list;
//            }else{
//                return (List<Employee>)obj;
//            }
//        }catch (Exception e){
//            throw new SysException("系统错误！",e);
//        }

//        Jedis jedis = new Jedis("localhost", 6379);
//        try{
//
//            byte[] key = empDto.toString().getBytes("UTF-8");
//
//            //从缓存中找是否有key，若有，直接从缓存获取返回，否则从从数据库中查，放入缓存中返回
//            byte[] objBytes = jedis.get(key);
//            if(objBytes == null){//缓存中不存在
//
//                List<Employee> list = empDao.findEmpList(empDto);
//                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//                ObjectOutputStream out = new ObjectOutputStream(byteArrayOutputStream);
//                out.writeObject(list);
//                out.flush();
//                out.close();
//
//                byte[] b = byteArrayOutputStream.toByteArray();
//                jedis.set(key, b);
//                jedis.expire(key, 10L);
//                return list;
//
//            }else{//缓存存在
//                ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(objBytes));
//                List<Employee> list = (List<Employee>) in.readObject();
//                in.close();
//                return list;
//            }
//
//
//        }catch (Exception e){
//            throw new SysException("系统错误！",e);
//        }finally{
//            jedis.close();
//        }
    }

    @Cacheable(key = "targetClass+'.'+methodName+'('+#eid+')'")
    @Override
    public Employee getEmp(String eid) {
        try{
            return empDao.findEmpById(eid);
        }catch (Exception e){
            throw new SysException("系统错误！",e);
        }

    }

    @CacheEvict(allEntries = true)//清空emp名下的所有缓存
    @Override
    public void addOneEmp(EmpDto empDto) {
        /**
         * 若工号已经存在，抛出业务异常
         *
         * 新增数据
         */

        boolean exists = false;//判断工号是否存在
        try {
            exists = empDao.findIsExistsEmpId(empDto.getE_id());
        } catch (Exception e) {
            throw new SysException("系统错误！",e);
        }
        if(exists){
            throw new BusinessException("您提交的工号已经存在！");
        }
        try {
            empDao.insertOneEmp(empDto);
        } catch (Exception e) {
            throw new SysException("系统错误！",e);
        }

    }

    @CacheEvict(allEntries = true)
    @Override
    public void updateEmp(EmpDto empDto) {
        try {
            empDao.updateEmp(empDto);
        } catch (Exception e) {
            throw new SysException("系统错误！",e);
        }
    }

    @CacheEvict(allEntries = true)
    @Override
    public void deleteEmp(String[] eids) {

        if(eids == null || eids.length == 0){
            throw new BusinessException("无选择的员工数据！");
        }

        try {
            empDao.deleteEmp(eids);
        } catch (Exception e) {
            throw new SysException("系统错误！",e);
        }
    }
}
